<?php

/**
 * RaiseABTestingArea UIControl class
 * Allow developers to do A/B Testing of UI and Content
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.UI
 * @since 1.2
 */
class RaiseABTestingArea extends RaiseUIControl {

    /**
     * The GoogleAnalytics object to interact and add custom variables to
     * @var GoogleAnalytics
     */
    private $googleAnalytics;

    /**
     * A RaiseCollection of all the variations to use in the A/B testing
     * @var RaiseCollection
     */
    private $variations;

    public function __construct($ga) {
        $this->googleAnalytics($ga);
        $this->variations = new RaiseCollection();
    }

    /**
     * Get or set the GoogleAnalytics object for use with the AB Testing
     * @param GoogleAnalytics $ga
     * @return GoogleAnalytics
     */
    public function googleAnalytics($ga = false){
        if($ga !== false){
            $this->googleAnalytics = $ga;
        }
        return $this->googleAnalytics;
    }

    /**
     * Add a variation to the testing area
     * @param string $id The identifier to uniquely identify this variation
     * @param string $content 
     */
    public function addVariation($id, $content){
        $this->variations->add($id, $content);
    }

    public function output() {
        $kvp = $this->variations->random();
        $this->googleAnalytics()->params()->add(array('_setCustomVar', 1, 'RaiseABTestingArea', $kvp->key()));
        return $kvp->value();
    }

}
